package com.wnxy.carl.carlorderassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.carl.carlorderassembly.entity.Order;
import com.wnxy.carl.carlorderassembly.mapper.OrderMapper;
import com.wnxy.carl.carlorderassembly.service.IOrderService;
import com.wnxy.carl.commons.DateUtil;
import com.wnxy.carl.dto.OrderDTO;
import com.wnxy.carl.dto.PageDTO;
import com.wnxy.carl.exception.DemoRuntimeException;
import com.wnxy.carl.param.PageParam;
import com.wnxy.carl.param.order.*;
import com.wnxy.carl.response.EPage;
import com.wnxy.carl.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-11-12
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Resource
    private OrderMapper orderMapper;

    //生成订单 OrderDTO
    public OrderDTO generateOrder(GenerateOrderParam generateOrderParam) {
        log.info("开始执行生成订单：参数={}",generateOrderParam);
        Order order = new Order();
        BeanUtils.copyProperties(generateOrderParam,order);
        order.setCreateTime(DateUtil.getSystemTimeStamp());
        int insert = orderMapper.insert(order);
        if (insert == 0) {
            throw new DemoRuntimeException("订单生成成功");
        }
        Order order1 = orderMapper.selectById(order.getOrderId());
        OrderDTO orderDTO = new OrderDTO();
        BeanUtils.copyProperties(order1,orderDTO);
        return seeOrderById(order.getOrderId());
    }
    //修改订单 OrderDTO
    public OrderDTO amendOrder(AmendOrderParam amendOrderParam) {
        log.info("开始执行修改订单功能：参数={}",amendOrderParam);
        Order order = orderMapper.selectById(amendOrderParam.getOrderId());
        if (null == order) {
            throw new DemoRuntimeException("这张订单不存在");
        }
        BeanUtils.copyProperties(amendOrderParam,order);
        int i = orderMapper.updateById(order);
        if (i == 0){
            throw new DemoRuntimeException("修改失败");
        }
        return seeOrderById(order.getOrderId());
    }
    //账户余额支付 Boolean
    public void balancePay(MoneyPayParam moneyPayParam) {
        log.info("开始执行账户余额支付功能：参数={}",moneyPayParam);
        Order order = orderMapper.selectById(moneyPayParam.getOrderId());
        if (null == order) {
            throw new DemoRuntimeException("这张订单不存在");
        }
        UpdateWrapper<Order> uw = new UpdateWrapper<>();
        if (null != moneyPayParam.getDeposit() && order.getPaymentMethod() == 2) {
            uw.set("deposit_status", 1);
        }else if (null != moneyPayParam.getOrderTotal()){
            uw.set("order_status",1);
        }else {
            throw new DemoRuntimeException("请支付金额");
        }
        uw.eq("order_id",moneyPayParam.getOrderId());
        int update = orderMapper.update(null, uw);
        if (update == 0){
            throw new DemoRuntimeException("修改失败");
        }
    }
    //用户查询订单 PageDTO<OrderDTO>
    public PageDTO<OrderDTO> myOrder(PageParam<MyOrderParam> pageParam) {
        log.info("开始执行用户查询订单功能：参数={}",pageParam);
        MyOrderParam data = pageParam.getData();
        Order order = new Order();
        BeanUtils.copyProperties(data,order);
        PageDTO<OrderDTO> pageDTO = dynamicQuery(pageParam.getPageIndex(), pageParam.getPageSize(), order, data.getStartTime(), data.getEndTime());
        return pageDTO;
    }

    //门店查询订单 PageDTO<OrderDTO>
    public PageDTO<OrderDTO> seeStoreOrder(PageParam<SeeStoreOrderParam> pageParam) {
        log.info("开始执行门店查询订单功能：参数={}",pageParam);
        SeeStoreOrderParam data = pageParam.getData();
        Order order = new Order();
        BeanUtils.copyProperties(data,order);
        PageDTO<OrderDTO> pageDTO = dynamicQuery(pageParam.getPageIndex(), pageParam.getPageSize(), order, null, null);
        return pageDTO;
    }
    //用于时间段订单信息的统计 StatisticsOrderDTO
    public PageDTO<OrderDTO> statisticsOrder(PageParam<StatisticsOrderParam> pageParam){
        log.info("开始执行用于时间段订单信息的统计功能：参数={}",pageParam);
        StatisticsOrderParam data = pageParam.getData();
        Order order = new Order();
        BeanUtils.copyProperties(data,order);
        PageDTO<OrderDTO> pageDTO = dynamicQuery(pageParam.getPageIndex(), pageParam.getPageSize(), order, data.getStartTime(), data.getEndTime());
        return pageDTO;
    }
    private OrderDTO seeOrderById(Integer orderId){
        Order order1 = orderMapper.selectById(orderId);
        OrderDTO orderDTO = new OrderDTO();
        BeanUtils.copyProperties(order1,orderDTO);
        return orderDTO;
    }

    private PageDTO<OrderDTO> dynamicQuery(Integer pageIndex,Integer pageSize,Order order,Long startTime,Long endTime){
        IPage<Order> page = new Page<>(pageIndex, pageSize);
        QueryWrapper<Order> qw = new QueryWrapper<>();
        if (null != order.getOrderId() && order.getOrderId() != 0){
            qw.eq("store_id", order.getStoreId());
        }
        if (null != order.getUserId() && order.getUserId() != 0){
            qw.eq("user_id", order.getUserId());
        }
        if (null != order.getOrderId() && order.getOrderId() != 0){
            qw.eq("order_id", order.getOrderId());
        }
        if (null != order.getSaleStatus() && order.getSaleStatus() != 0){
            qw.eq("sale_status", order.getSaleStatus());
        }
        if (null != order.getOrderStatus() && order.getOrderStatus() != 0){
            qw.eq("order_status", order.getOrderStatus());
        }if (null != order.getReceivingStatus() && order.getReceivingStatus() != 0){
            qw.eq("receiving_status", order.getReceivingStatus());
        }
        if (null != order.getOrderNumber()){
            qw.eq("order_number", order.getOrderNumber());
        }
        if (null != startTime && null != endTime  && startTime != 0 && endTime != 0){
            qw.gt("create_time", startTime);
            qw.lt("create_time",endTime);
        }
        IPage<Order> orderIPage = orderMapper.selectPage(page, qw);
        PageDTO<OrderDTO> pageDTO = PageDTO.bulid((Page<?>) orderIPage, OrderDTO.class);
        return pageDTO;
    }

}
